package com.canbot.u05.activity.vipguide;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.SPUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 *设置贵宾接待动作--选择舞蹈
* */
public class ReceptionSettingDanceActivity extends BaseActivityNoTitle implements AdapterView.OnItemClickListener {

        private List<String> danceList = new ArrayList<>();

        private ReceptionDanceGvAdapter receptionDanceGvAdapter;

        String[] danceNames={"龙拳","杰克逊舞蹈","小苹果",
                "无法停止的感觉","沧海一声笑","最炫民族风","海草海草","DESPACITO","TROUBLE MAKER","红昭愿","蓝色多瑙河"};

        private GridView gridView;

        private int chooseIndex = -1;

        private String danceName;

        private String exitDanceName;

        private boolean isItemClick =false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.act_reception_setting_dance);
                setViews();
                exitDanceName = getIntent().getStringExtra("exitDanceName");

        }

        private void setViews() {
                Collections.addAll(danceList, danceNames);

                gridView = findViewById(R.id.gv_vip_reception);
                receptionDanceGvAdapter = new ReceptionDanceGvAdapter();
                gridView.setAdapter(receptionDanceGvAdapter);
                gridView.setOnItemClickListener(this);
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        @Override
        protected void onPause() {
                super.onPause();
                Logger.e("1235",danceName+"onPause");
                if(!isItemClick){
                        SPUtils.saveString(ReceptionSettingDanceActivity.this,"danceName",exitDanceName);
                }else{
                        SPUtils.saveString(ReceptionSettingDanceActivity.this,"danceName",danceName);
                }
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                isItemClick = true;
                if (chooseIndex == position) {
                        chooseIndex = -1;
                        danceName = null;
                }
                else {
                        chooseIndex = position;
                        danceName = danceList.get(position);
                }
                exitDanceName = null;
                receptionDanceGvAdapter.notifyDataSetChanged();
        }


        class ReceptionDanceGvAdapter extends BaseAdapter {

                @Override
                public int getCount() {
                        return danceList.size();
                }

                @Override
                public Object getItem(int position) {
                        return danceList.get(position);
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;
                        if (convertView == null) {
                                convertView = View.inflate(ReceptionSettingDanceActivity.this, R.layout.gridview_vipguide_recrption_dance, null);
                                holder = new ViewHolder();
                                holder.danceName = convertView.findViewById(R.id.name);

                                convertView.setTag(holder);
                        }
                        else {
                                holder = (ViewHolder) convertView.getTag();
                        }

                        holder.danceName.setText(danceList.get(position));
                        if (!TextUtils.isEmpty(exitDanceName)) {
                                if (danceList.get(position).equals(exitDanceName)) {
                                        chooseIndex = position;
                                }
                        }

                        if (position == chooseIndex) {
                                holder.danceName.setTextColor(android.graphics.Color.parseColor("#ffffff"));
                                holder.danceName.setBackgroundResource(R.drawable.vip_dance_bg_sel);
                        }
                        else {
                                holder.danceName.setTextColor(android.graphics.Color.parseColor("#fb8853"));
                                holder.danceName.setBackgroundResource(R.drawable.vip_dance_bg);
                        }

                        return convertView;
                }

                class ViewHolder {

                        TextView danceName;

                }
        }
}
